let joblistModule = (function(){
    let $tbody = $('tbody');
    // 渲染裂变
    function bindjobList(result){
        let str = '';
        result.forEach((item)=>{
            str += `<tr>
            <td class="w8">${item.id}</td>
            <td class="w10">${item.name}</td>
            <td class="w20">${item.desc}</td>
            <td class="w50">${item.power}</td>
            <td class="w12" data-jobId="${item.id}">
                <a href="javascript:;">编辑</a>
                <a href="javascript:;">删除</a>
            </td>
        </tr>`
        });
        $tbody.html(str);
    }

    // 基于事件委托处理编辑和删除
    function delegate(){
        // console.log('hhh')
        $tbody.click(async (ev)=>{
            // console.log('ev');
            let target = ev.target||window.event.target,
                $target = $(target),
                TAG = target.tagName,
                TEXT = target.text;
                if(TAG==='A'){
                    // console.log('a')
                    let jobId = $target.parent().attr('data-jobId');
                    if(TEXT==='编辑'){
                        console.log('编辑')
                        window.location.href = `jobadd.html?jobId=${jobId}`;
                        return;
                    }
                    if(TEXT==='删除'){
                        let flag = confirm(`确定删除编号为【${jobId}】的部门信息吗？无法撤销哦！`)
                        if(!flag) return;
                        let result = await axios.get('/job/delete',{
                            params:{
                                jobId
                            }
                        });
                        if(result.code==0){
                            alert('此条信息已删除');
                            $target.parent().parent().remove();
                            return;
                        }
                    }
                    alert('网络繁忙，请稍后重试');
                    return;
                }
        })
    }

    return {
        async init(){
            let result = await axios.get('/job/list');
            if(result.code==0){
                bindjobList(result.data);
            }
            // console.log('hhh')
            delegate();
        }
    }
})();
joblistModule.init();